package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.R;
import com.example.myapplication.activity.VideoPlayActivity;
import com.example.myapplication.entity.RecommendVideo;

import java.util.List;

public class VideoRecommendItemAdapter extends RecyclerView.Adapter<VideoRecommendItemAdapter.VideoRecommendItemHolder> {
    private Context context;
    private List<RecommendVideo.DatasBean> datasBeanList;

    public VideoRecommendItemAdapter(Context context) {
        this.context = context;
    }
    public void setData(List<RecommendVideo.DatasBean> datasBeanList){
        this.datasBeanList = datasBeanList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public VideoRecommendItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_recommend_item,parent,false);
        return new VideoRecommendItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VideoRecommendItemHolder holder, int position) {
        RecommendVideo.DatasBean datasBean = datasBeanList.get(position);
        holder.card_title.setText(datasBean.getData().getTitle());
        //holder.card_text.setText(datasBean.getData().getCreator().getSignature());
        RoundedCorners roundedCorners= new RoundedCorners(30);
        RequestOptions options= RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
        Glide.with(context).load(datasBean.getData().getCoverUrl())
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.drawable.ic_baseline_refresh_24)
                .placeholder(R.drawable.ic_baseline_refresh_24)
                .apply(options)
                .into(holder.card_img);
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, VideoPlayActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("video",datasBeanList.get(position));
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datasBeanList==null?0:datasBeanList.size();
    }

    class VideoRecommendItemHolder extends RecyclerView.ViewHolder {
        TextView card_title;
        CardView cardView;
        TextView card_text;
        ImageView card_img;
        public VideoRecommendItemHolder(@NonNull View itemView) {
            super(itemView);
            card_title = itemView.findViewById(R.id.card_title);
            cardView = itemView.findViewById(R.id.cardView);
            card_text = itemView.findViewById(R.id.card_text);
            card_img = itemView.findViewById(R.id.card_img);

        }
    }
}
